﻿using System;

namespace test
{
    class Demo
    {
        static void Main(string[] args)
        {
            Messenger messenger = new Messenger();
            Stock stock = new Stock(messenger);
            System.Console.WriteLine("1------");
            stock.Price = 10;
            System.Console.WriteLine("2------");
            stock.Price = 20;
        }
    }
    interface IMessenger
    {
        void alert();
    }
    class Messenger : IMessenger
    {
        public void alert() => System.Console.WriteLine("price属性变化");
    }
    class Stock
    {
        private int? _price;

        public int? Price
        {
            get { return _price; }
            // set { _price = value; }
            set
            {
                if (_price != value)
                {
                    _price = value;
                    messenger.alert();
                }
            }
        }
        private IMessenger messenger;
        public Stock(IMessenger messenger)
        {
            this.messenger = messenger;
        }
    }
}